// +----------------------------------------------------------------------
// | JavaWeb_EleVue_Pro前后端分离旗舰版框架 [ JavaWeb ]
// +----------------------------------------------------------------------
// | 版权所有 2021 上海JavaWeb研发中心
// +----------------------------------------------------------------------
// | 官方网站: http://www.javaweb.vip/
// +----------------------------------------------------------------------
// | 作者: 鲲鹏 <javaweb520@gmail.com>
// +----------------------------------------------------------------------
// | 免责声明:
// | 本软件框架禁止任何单位和个人用于任何违法、侵害他人合法利益等恶意的行为，禁止用于任何违
// | 反我国法律法规的一切平台研发，任何单位和个人使用本软件框架用于产品研发而产生的任何意外
// | 、疏忽、合约毁坏、诽谤、版权或知识产权侵犯及其造成的损失 (包括但不限于直接、间接、附带
// | 或衍生的损失等)，本团队不承担任何法律责任。本软件框架只能用于公司和个人内部的法律所允
// | 许的合法合规的软件产品研发，详细声明内容请阅读《框架免责声明》附件；
// +----------------------------------------------------------------------

package com.javaweb.admin.service.impl;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.javaweb.admin.entity.Identify;
import com.javaweb.admin.mapper.IdentifyMapper;
import com.javaweb.common.common.BaseQuery;
import com.javaweb.common.common.BaseServiceImpl;
import com.javaweb.admin.entity.IdentifyData;
import com.javaweb.admin.mapper.IdentifyDataMapper;
import com.javaweb.admin.query.IdentifyDataQuery;
import com.javaweb.admin.service.IIdentifyDataService;
import com.javaweb.common.utils.StringUtils;
import com.javaweb.system.entity.Article;
import com.javaweb.system.mapper.ArticleMapper;
import com.javaweb.system.service.IItemCateService;
import com.javaweb.admin.vo.identifydata.IdentifyDataInfoVo;
import com.javaweb.admin.vo.identifydata.IdentifyDataListVo;
import com.javaweb.common.utils.DateUtils;
import com.javaweb.common.utils.JsonResult;
import com.javaweb.system.vo.article.ArticleInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.*;

/**
  * <p>
  * 识别记录数据表 服务类实现
  * </p>
  *
  * @author 鲲鹏
  * @since 2023-09-01
  */
@Service
public class IdentifyDataServiceImpl extends BaseServiceImpl<IdentifyDataMapper, IdentifyData> implements IIdentifyDataService {

    @Autowired
    private IdentifyDataMapper identifyDataMapper;

    @Autowired
    private IdentifyMapper identifyMapper;
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private IItemCateService itemCateService;
    /**
     * 获取数据列表
     *
     * @param query 查询条件
     * @return
     */
    @Override
    public JsonResult getList(BaseQuery query) {
        IdentifyDataQuery identifyDataQuery = (IdentifyDataQuery) query;
        // 查询条件
        QueryWrapper<IdentifyData> queryWrapper = new QueryWrapper<>();
        // 
        if (!StringUtils.isEmpty(identifyDataQuery.getName())) {
            queryWrapper.like("name", identifyDataQuery.getName());
        }

        if (identifyDataQuery.getStatus()!=null) {
            queryWrapper.eq("status", identifyDataQuery.getStatus());
        }
        if (identifyDataQuery.getLikes()==1) {
            queryWrapper.lt("likes", 0.7);
        }
        if (identifyDataQuery.getLikes()==2) {
            queryWrapper.gt("likes", 0.7).lt("likes",0.8);
        }
        if (identifyDataQuery.getLikes()==3) {
            queryWrapper.gt("likes", 0.8).lt("likes",0.9);
        }
        if (identifyDataQuery.getLikes()==4) {
            queryWrapper.gt("likes", 0.9);
        }

        //时间检索
        if (com.javaweb.common.utils.StringUtils.isNotNull(identifyDataQuery.getTime())) {// 按指定时间检索
            String[] arr = identifyDataQuery.getTime().split(",");
            queryWrapper.between("create_time",DateUtils.getStartDate(DateUtils.parseDate(arr[0]), true),DateUtils.getFinallyDate(DateUtils.parseDate(arr[1])));
        }
        queryWrapper.eq("mark", 1);
        queryWrapper.orderByDesc("id");

        // 获取数据列表
        IPage<IdentifyData> page = new Page<>(identifyDataQuery.getPage(), identifyDataQuery.getLimit());
        IPage<IdentifyData> pageData = identifyDataMapper.selectPage(page, queryWrapper);
        pageData.convert(x -> {
            IdentifyDataListVo identifyDataListVo = Convert.convert(IdentifyDataListVo.class, x);

            QueryWrapper<Article> queryWrapper2 = new QueryWrapper<>();
            queryWrapper2.like("title", x.getName());
            List<Article> article = articleMapper.selectList(queryWrapper2);
            ArticleInfoVo articleInfoVo = new ArticleInfoVo();
            // 所属栏目
            if (article.get(0).getCateId() > 0) {
                String cateName = itemCateService.getCateName(article.get(0).getCateId(), ">>");
                articleInfoVo.setCateName(cateName);
            }
            // 拷贝属性
            BeanUtils.copyProperties(article.get(0), articleInfoVo);
            identifyDataListVo.setArticle(articleInfoVo);
            Identify identify = identifyMapper.selectById(x.getPid());
            identifyDataListVo.setDentify(identify);
            return identifyDataListVo;
        });
        return JsonResult.success(pageData);
    }

    /**
     * 获取详情Vo
     *
     * @param id 记录ID
     * @return
     */
    @Override
    public Object getInfo(Serializable id) {
        IdentifyData entity = (IdentifyData) super.getInfo(id);
        // 返回视图Vo
        IdentifyDataInfoVo identifyDataInfoVo = new IdentifyDataInfoVo();
        // 拷贝属性
        BeanUtils.copyProperties(entity, identifyDataInfoVo);
        return identifyDataInfoVo;
    }

    /**
     * 添加、更新记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult edit(IdentifyData entity) {
        if (StringUtils.isNotNull(entity.getId()) && entity.getId() > 0) {
        } else {
        }
        return super.edit(entity);
    }

    /**
     * 删除记录
     *
     * @param entity 实体对象
     * @return
     */
    @Override
    public JsonResult delete(IdentifyData entity) {
        entity.setMark(0);
        return super.delete(entity);
    }

}